package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.mess.UpOrDownMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArtilceIsDownListener {

    @Autowired
    ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMsg(String msg){
        log.info("消费到消息：{}",msg);

        UpOrDownMess upOrDownMess = JSON.parseObject(msg, UpOrDownMess.class);
        //修改表ap_article_config  表 is_down      0上架  1下架
        //update ap_article_config set is_down =  0/1  where article_id=xxxx
        apArticleConfigMapper.update(null, Wrappers.<ApArticleConfig>lambdaUpdate()
                .set(ApArticleConfig::getIsDown, upOrDownMess.getEnable()==1?0:1 )
                .eq(ApArticleConfig::getArticleId, upOrDownMess.getArticleId()));
    }

}
